<?php

class BaseController extends Controller {

	/**
	 * Setup the layout used by the controller.
	 *
	 * @return void
	 */
	protected function setupLayout()
	{
		//Log::error($agent);
		if ( !is_null($this->layout))
		{
			$this->layout = $this->makeView($this->layout);
		}
	}

	protected function makeView($view){
		$fields = explode(".",$view);
		//如果是手机终端，则返回手机端的模版
		if ($this->isMobile()){
			$first = $fields[0];
			if ($first == "layouts"){
				$fields[1] = Config::get("mao.terminal");//"mobile";
			}
			if ($first == "front"){
				$fields[0] = Config::get("mao.terminal");
			}
			$mview =  implode(".", $fields);
			return View::make($mview);
		}
		return View::make($view);
	}
	
	protected function isMobile(){
		$agent = Request::header('user-agent');
		if (isset($agent)){
			$devices = array ('iphone','ipod','android','ios','ipad');
			$pattern = '/('.implode('|', $devices).')/';
			$value = preg_match($pattern, strtolower($agent));
			return $value;
		} else {
			return false;
		}
	}
	
	protected function isWeixin(){
		$agent = Request::header('user-agent');
		if ( strpos($agent, 'MicroMessenger') !== false ) {
			return true;
		}	
		return false;
		//Mozilla/5.0 (iPhone; CPU iPhone OS 7_1_1 like Mac OS X) AppleWebKit/537.51.2 (KHTML, like Gecko) Mobile/11D201 MicroMessenger/6.3.8 NetType/WIFI Language/zh_CN
	}
}
